package com.ailink.concurrent.thread;

/**
 * Created by Administrator on 2018/9/26
 */
public class ThreadWaitAndNotify {

    private static Object lock = new Object();

    public static class Thread1 extends Thread {
        @Override
        public void run() {
            System.out.println("1 start");
            synchronized (lock) {
                System.out.println("1 running");
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("1 end");
        }
    }

    public static class Thread2 extends Thread {
        @Override
        public void run() {
            System.out.println("2 start");
            synchronized (lock) {
                System.out.println("2 running");
                lock.notify();
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("2 end");
        }
    }

    public static void main(String[] args) {
        Thread thread1 = new ThreadWaitAndNotify.Thread1();
        Thread thread2 = new ThreadWaitAndNotify.Thread2();
        thread1.start();
        try {
            Thread.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        thread2.start();
    }
}
